package net.csdn.business.mirror.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import net.csdn.business.common.domain.query.mirror.ThirdPartInfoMirrorPageQuery;
import net.csdn.business.common.domain.vo.mirror.ProjectMirrorDataVO;
import net.csdn.business.common.domain.vo.mirror.ThirdPartInfoListVO;
import net.csdn.business.mirror.entity.bean.ThirdPartMirrorInfo;
import net.csdn.business.mirror.mapper.ThirdPartMirrorInfoMapper;
import net.csdn.business.mirror.service.ThirdPartMirrorInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class ThirdPartMirrorInfoServiceImpl implements ThirdPartMirrorInfoService {

    @Autowired
    private ThirdPartMirrorInfoMapper thirdPartMirrorInfoMapper;

    @Override
    public Page<ThirdPartInfoListVO> list(ThirdPartInfoMirrorPageQuery query) {
        Page<ThirdPartInfoListVO> voPage= query.getPage();
        // 分页查询
        Page<ThirdPartMirrorInfo> page = new Page<>(query.getPageNum(), query.getPageSize());
        LambdaQueryWrapper<ThirdPartMirrorInfo> queryWrapper = new QueryWrapper<ThirdPartMirrorInfo>().lambda();
        queryWrapper.like(StringUtils.isNotBlank(query.getName()),ThirdPartMirrorInfo::getName,query.getName())
                .like(StringUtils.isNotBlank(query.getSource()),ThirdPartMirrorInfo::getSource,query.getSource())
                .like(StringUtils.isNotBlank(query.getPlatform()),ThirdPartMirrorInfo::getPlatform,query.getPlatform());
        IPage<ThirdPartMirrorInfo> result = thirdPartMirrorInfoMapper.selectPage(page,queryWrapper);
        List<ThirdPartMirrorInfo> records = result.getRecords();
        List<ThirdPartInfoListVO> resultVOList = new ArrayList<>();
        for (int i = 0; i < records.size(); i++) {
            ThirdPartMirrorInfo thirdPartMirrorInfo =  records.get(i);
            ThirdPartInfoListVO thirdPartInfoListVO = new ThirdPartInfoListVO();
            BeanUtils.copyProperties(thirdPartMirrorInfo, thirdPartInfoListVO);
            resultVOList.add(thirdPartInfoListVO);
        }
        voPage.setRecords(resultVOList);
        voPage.setTotal(result.getTotal());
        voPage.setPages(result.getPages());
        return voPage;
    }
}
